
function Site(){}
Site.uploadQueue = [];
Site.upload = function(form,handle){
	if (form.action=='processing'){
		return;
	}
		
	if (this.uploadFrame == null)
	{
		this.uploadFrame = this.createIFrame('uploadFrame','Site.onUploadOK()');
	}
	var action = form.action;
	var target = form.target;
	var task = {'form':form, 'target': target, 'action':action, 'handle':handle};
	if (this.currentUpload!=null)
	{
		this.uploadQueue.push(task);
	}
	else
		this.currentUpload = task;
	form.target = this.uploadFrame.name;
	try{
		form.submit();
	}catch(e){
		this.uploadQueue = [];
		this.currentUpload = null;
		this.uploadFrame = null;
		alert("上传失败:"+e.description);
	}
	form.action = 'processing';
}

Site.onUploadOK = function(){
	if (this.currentUpload==null)
		return;
	var task = this.currentUpload;
	var form = task.form;
	form.target = task.taget;
	form.action = task.action;
	
	var result = this.uploadFrame.contentWindow.document.body.innerHTML;
	
	this.currentUpload = null;
	
	if (this.uploadQueue.length>0)
	{
		var task2 = this.uploadQueue.shift();
		this.currentUpload = task2;
		var form2 = task2.form;
		form2.target = this.uploadFrame.name;
		form2.submit();
		form2.action = null;
	}
	if (task.handle!=null)
		task.handle(result);
}


Site.createIFrame = function(fname, onloadstr){
	if (onloadstr==null) onloadstr = '';
	if(window[fname]){ return window[fname]; }
	if(window.frames[fname]){ return window.frames[fname]; }
	var cframe = null;
	var ifrstr = (this.isIE()) ? "<iframe name='"+fname+"' src='' onload='"+onloadstr+"'>" : "iframe";
	cframe = document.createElement(ifrstr);
	with(cframe){
		name = fname;
		id = fname;
	}
	(document.body||document.getElementsByTagName("body")[0]).appendChild(cframe);
	window[fname] = cframe;
	with(cframe.style){
		position = "absolute";
		left = top = "0px";
		height = width = "1px";
		visibility = "hidden";
	}

	if(!this.isIE()&&onloadstr!=''){
		cframe.onload = new Function(onloadstr);
	}
	
	return cframe;
}
Site.download = function(){}
Site.isIE = function()
{
	if (this._isIE==null)
	{
		this._isIE = false;
		if(navigator.appName == "Microsoft Internet Explorer")
			this._isIE=true;
	}
	return this._isIE;
}

Site.previewPic = function(obj,view){
	$(view).innerHTML = "<img id='"+view+"_img' src='' width='120'>";
	if(obj.value){
		document.getElementById(view+"_img").src = obj.value
	}else
		document.getElementById(view+"_img").src = "";
}

Site.createHidden = function(formObj,inputName){
	if (!formObj[inputName]){
		var input = document.createElement("INPUT")
		input.id = inputName
		input.name = inputName
		input.type = "hidden"
		formObj.appendChild(input)
	}
}

Site.uploadPic = function(form,entityType,entityId,entityName,callback){

	var formObj = typeof form!='object'?$(form):form;
	
	if (!entityId||!entityType){
		showError("无"+(entityName?entityName:"")+"信息，不能上传图片");
		return
	}	
	if (!formObj.photoFile||!formObj.photoFile.value){
		showError("请选择一张图片上传");
		return
	}	
	formObj.action= contextRoot + "d/upload/photo";

	Site.createHidden(formObj,"entityId")
	formObj.entityId.value = entityId
	
	Site.createHidden(formObj,"entityType")
	formObj.entityType.value = entityType
	
	Site.createHidden(formObj,"entityName")
	formObj.entityName.value = entityName
	
	Site.upload(formObj,function(r){
		var obj;
		if (r)
			eval("obj="+r)
		else
			obj = {}
		if (obj.photoId){
			showInformation("图片保存成功！");
		}else{
			if (obj.error){
				showError(obj.error)
			}
		}
			
		if (callback)
			callback(obj)
	});	
}


Site.uploadProductionResultFile = function(form, callback){
	var formObj = typeof form!='object'?$(form):form;
	
	Site.upload(formObj,function(r){
		var obj;
		if(r)
			eval("obj="+r)
		else
			obj = {}
		//alert(obj);
		//alert(obj.serverFileName);
			
		if (obj.serverFileName){
			//showInformation("图片保存成功！");
			if (callback)
				callback(obj.serverFileName)
		}else{
			if (obj.error){
				showError(obj.error)
			}
		}	
	});	
}